<template>
    <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="155px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="日期" prop="logDate">
                        <el-input v-model="form.logDate" placeholder="日期" :disabled="true" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="顾问" prop="empName">
                        <el-input v-model="empName" placeholder="顾问" :disabled="true" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="部门归属" prop="orgName">
                        <el-input v-model="orgName" placeholder="部门归属" :disabled="true" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="今日上门" prop="searchVisit">
                        <el-input placeholder="请输入姓名或手机号查询" v-model="searchVisit" class="input-with-select">
                            <el-button slot="append" icon="el-icon-search" @click="selectCustInfo(1)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-card class="box-card cust-box">
                    <div slot="header" class="clearfix">
                        <span>请选择添加上门客户</span>
                    </div>
                    <div v-for="item in todayVisitlist" class="cust-box-opera">
                        <div class="custName-div">{{ item.custName }}</div>
                        <div><el-input v-model="item.custDesc" placeholder="详情描述" style="width: 350px;"></el-input> </div>
                        <div> <el-button type="primary" icon="el-icon-delete" @click="delVisitCust(item)"
                                size="mini"></el-button></div>

                        <hr color="#dcdfe6">
                    </div>
                </el-card>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="今日签约" prop="searchSigning">
                        <el-input placeholder="请输入姓名或手机号查询" v-model="searchSigning" class="input-with-select">
                            <el-button slot="append" icon="el-icon-search" @click="selectCustInfo(2)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="今日签约率" prop="todaySignedRate">
                        <el-input v-model="form.todaySignedRate" placeholder="%" :disabled="true" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-card class="box-card cust-box">
                    <div slot="header" class="clearfix">
                        <span>请选择添加签约客户</span>
                    </div>
                    <div v-for="item in todaySignedlist" class="cust-box-opera">
                        <div class="custName-div">{{ item.custName }}</div>
                        <div><el-input v-model="item.custDesc" placeholder="详情描述" style="width: 350px;"></el-input> </div>
                        <div>
                            <el-select v-model="item.related" placeholder="是否有关联客户" style="width: 100px;" clearable>
                                <el-option label="关联客户情况" value=""></el-option>
                                <el-option label="是" value="1"></el-option>
                                <el-option label="否" value="2"></el-option>
                            </el-select>
                        </div>
                        <div v-if="item.related == 1">
                            <el-input placeholder="请输入姓名或手机号查询" v-model="item.relatedcustname2" style="width: 150px;"
                                class="input-with-select">
                            </el-input>
                        </div>
                        <div v-if="item.related == 1">
                            <el-button type="primary" @click="selectCust(item)" size="mini">选择关联客户</el-button>
                        </div>
                        <div v-if="item.related == 1"> <el-input placeholder="姓名-关联后显示" v-model="item.relatedcustname"
                                class="input-with-select" :disabled="true">
                            </el-input>
                        </div>
                        <div> <el-button type="primary" icon="el-icon-delete" @click="delSignedCust(item)"
                                size="mini"></el-button></div>
                        <hr color="#dcdfe6">
                    </div>
                </el-card>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="今日进件" prop="searchIncoming">
                        <el-input placeholder="请输入姓名或手机号查询" v-model="searchIncoming" class="input-with-select">
                            <el-button slot="append" icon="el-icon-search" @click="selectCustInfo(3)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-card class="box-card cust-box">
                    <div slot="header" class="clearfix">
                        <span>请选择添加进件客户</span>
                    </div>
                    <div v-for="item in todayIncomings" class="cust-box-opera">
                        <div class="custName-div">{{ item.custName }}</div>
                        <div><el-button type="primary" @click="addIncomingCust(item)" size="mini">进件录入</el-button> </div>

                        <div> <el-button type="primary" icon="el-icon-delete" @click="delIncomingCust(item)"
                                size="mini"></el-button></div>
                        <hr color="#dcdfe6">
                    </div>
                </el-card>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="明日预计上门数" prop="tomorrowVisitCount">
                        <el-input v-model.number="form.tomorrowVisitCount" placeholder="请输入明日预计上门数" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="微信好友今日新增数量" prop="wechatFriendsTodayadd">
                        <el-input v-model.number="form.wechatFriendsTodayadd" placeholder="请输入微信好友今日新增数量" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="今日短信跟进客户数量" prop="todaySmsfollowCount">
                        <el-input v-model.number="form.todaySmsfollowCount" placeholder="今日短信跟进客户数量" />
                    </el-form-item>

                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="今日进件小贷客户数" prop="todayIntroductionCount">
                        <el-input v-model.number="form.todayIntroductionCount" placeholder="以贷款APP录入客户数计算" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="今日小贷担保人上门数" prop="todayIntroductionVisit">
                        <el-input v-model.number="form.todayIntroductionVisit" placeholder="请输入今日小贷担保人上门数" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="今日荐介客户签约数" prop="introductionSignedCount">
                        <el-input v-model.number="form.introductionSignedCount" placeholder="今日荐介客户签约数" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="今日通时" prop="todayCallTime">
                        <el-input v-model.number="form.todayCallTime" placeholder="今日通时" />
                    </el-form-item>
                </el-col>
            </el-row>

            <template
                v-if="resMap.company_leader == 1 || resMap.sub_leader == 1 || resMap.area_leader == 1 || resMap.depart_leader == 1 || resMap.is_xingzheng == 1">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="帮谈客户次数" prop="todayTalkCount">
                            <el-input v-model.number="form.todayTalkCount" placeholder="帮谈客户次数" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="分配客户次数" prop="todayDistributeCount">
                            {{ form.todayDistributeCount }}
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="分配总量" prop="todayDistributeTotal">
                            {{ form.todayDistributeTotal }}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="加入必跟进池次数" prop="todayAddmustCount">
                            {{ form.todayAddmustCount }}
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="加入总量" prop="todayAddmustTotal">
                            {{ form.todayAddmustTotal }}
                        </el-form-item>
                    </el-col>
                </el-row>
            </template>
            <el-row>
                <el-col :span="24" align="center">
                    <el-form-item>
                        <el-button type="primary" size="mini" @click="submitForm">提交日志</el-button>
                        <el-button type="danger" size="mini" @click="cancel">关闭</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <!-- 选择客户 -->
        <el-dialog :title="title"  :visible.sync="open" width="800px" append-to-body>
            <select-cust v-on:receivingResults="receivingResults" :condition="condition"></select-cust>
        </el-dialog>
        <!-- 添加客户进件信息 -->
        <el-dialog :title="title" :visible.sync="incomingOpen" width="90%" append-to-body>
            <incoming-edit  v-on:receivingIncoming="receivingIncoming" ref="childIncoming" :custInfo="custInfo"></incoming-edit>
        </el-dialog>

        
    </div>
</template>

<script>

import selectCust from "@/views/crmsys/custinfo/myCust/selectCust";//选择客户
import incomingEdit from "@/views/crmsys/incoming/edit/index";//添加进件信息
import {
    getPublicCustSet
} from "@/api/crmsys/custinfo/custinfo";

import { addBusinesslog, updateBusinesslog, getBusinesslog } from "@/api/crmsys/businesslog";
export default {
    components: { selectCust,incomingEdit },
    data() {
        return {
            todayVisitlist: [],//今日上门
            todaySignedlist: [],//今日签约
            todayIncomings: [],//今日进件
            form: {
                params: {}

            },
            open: false,
            custInfo:{},
            incomingOpen:false,
            condition: {}, //查询条件
            searchVisit: null,
            searchSigning: null,
            searchIncoming: null,
            type: null,
            type: null,
            title: "选择客户",
            rules: {
                tomorrowVisitCount: [{ required: true, message: '请输入明日预计上门数' }, { type: 'number', message: '明日预计上门数必须为数字值' }],
                wechatFriendsTodayadd: [{ required: true, message: '请输入微信好友今日新增数量' }, { type: 'number', message: '微信好友今日新增数量必须为数字值' }],
                todaySmsfollowCount: [{ required: true, message: '请输入今日短信跟进客户数量' }, { type: 'number', message: '今日短信跟进客户数量必须为数字值' }],
                todayIntroductionCount: [{ required: true, message: '请输入今日进件小贷客户数' }, { type: 'number', message: '今日进件小贷客户数必须为数字值' }],
                todayIntroductionVisit: [{ required: true, message: '请输入今日小贷担保人上门数' }, { type: 'number', message: '今日小贷担保人上门数必须为数字值' }],
                introductionSignedCount: [{ required: true, message: '请输入今日荐介客户签约数' }, { type: 'number', message: '今日荐介客户签约数必须为数字值' }],
                todayCallTime: [{ required: true, message: '请输入今日通时' }, { type: 'number', message: '今日通时必须为数字值' }],

            },
            todaySigned: {},//要添加关联客户的对象
            resMap: {},
            orgName: null,
            empName: null
        }
    },
    created() {
        this.getPublicCustSet();
        this.getBusinessLogInfo();
    },
    mounted() {

    },
    methods: {
        getBusinessLogInfo() {
            const id = this.$route.query.id;
            if (id == null || id == "") {
                //新增数据
                const type = this.$route.query.type;
                this.form.logDate = this.getDate(type);
                this.form.empId = this.$store.state.user.id;
                this.form.orgId = this.$store.state.user.deptid;
                this.orgName = this.getDeptByOrgId(this.form.orgId);
                this.empName = this.getNameByEmpId(this.form.empId);
                this.form.empName = this.getNameByEmpId(this.form.empId);
                this.form.wechatFriendsTotal = 0;
                this.form.todayNewcustCount = 0;
            } else {
                getBusinesslog(id).then(response => {
                    this.form = response.data;

                    if (this.form.todayVisitContent) {
                        this.todayVisitlist = JSON.parse(this.form.todayVisitContent);
                    }
                    if (this.form.todaySignedContent) {
                        this.todaySignedlist = JSON.parse(this.form.todaySignedContent);
                    }
                    if (this.form.todayIncomingContent) {
                        this.todayIncomings = JSON.parse(this.form.todayIncomingContent);
                    }

                });
            }
        },
        getDate(type) {
            let currentDate = new Date();
            if (type == "yesterday") {
                currentDate = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000); // 减去一天的毫秒数
            }
            let year = currentDate.getFullYear();
            let month = currentDate.getMonth() + 1; // 月份是从0开始的，所以要加1
            let day = currentDate.getDate();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            return year + "-" + month + "-" + day;
        },
        submitForm() {
            let canSubmit = true;
            this.todayVisitlist.forEach(data => {
                if (data.id == null || data.id == "" || data.custDesc == null || data.custDesc == "") {
                    canSubmit = false;
                }
            });
            if (!canSubmit) {
                this.$message.error("今日上门客户备注搜索不能为空");
                return;
            }
            this.todaySignedlist.forEach(data => {
                if (data.id == null || data.id == "" || data.custDesc == null || data.custDesc == "") {
                    canSubmit = false;
                }
            });
            if (!canSubmit) {
                this.$message.error("今日签约客户备注搜索不能为空");
                return;
            }
            this.form.todayVisitContent = JSON.stringify(this.todayVisitlist);
            this.form.todaySignedContent = JSON.stringify(this.todaySignedlist);
            this.form.todayIncomingContent = JSON.stringify(this.todayIncomings);
            this.$refs["form"].validate(valid => {
                if (valid) {

                    let message = "新增成功";
                    if (this.form.id != null) {
                        message = "修改成功";
                    }
                    addBusinesslog(this.form).then(response => {
                        console.log(response);
                        if (response.code == 200 && response.data == null) {
                            this.$modal.msgSuccess(message);
                            this.gotoList();
                        } else {
                            const errorMessage= response.data.msg;
                            let message2="";
                            let i=0;
                            errorMessage.forEach(element => {
                                i+=1;
                                message2=message2+"<p>"+i+","+element+"<p/>";
                            });
                            this.$alert(message2, '保存失败', {
                                dangerouslyUseHTMLString: true
                            });
                        }
                    });
                }
            });

        },
        cancel() {
            this.gotoList();
        },
        gotoList() {
            //跳转到List页面
            const obj = { path: this.$route.query.routePath, query: { t: Date.now(), queryParams: this.$route.query.queryParams, pageNum: this.$route.query.pageNum, custOpType: this.$route.query.custOpType } };
            this.$tab.closeOpenPage(obj);
        },
        selectCustInfo(type) {
            //选择客户
            let conditionText = "";
            if (type == 1) {
                //今日上门                
                conditionText = this.searchVisit;
            }
            if (type == 2) {
                //今日签约               
                conditionText = this.searchSigning;
            }
            if (type == 3) {
                //今日进件
                conditionText = this.searchIncoming;
            }
            if (conditionText == null || conditionText == "") {
                this.$message.error("请输入客户姓名或手机号，再点击查询");
                return;
            }
            this.condition = {};
            if (!/^1[3456789]\d{9}$/.test(conditionText)) {
                this.condition.custName = conditionText;
                this.condition.mobile = null;
            } else {
                this.condition.custName = null;
                this.condition.mobile = conditionText;
            }
            this.open = true;
            this.type = type;
            this.title = "选择客户";
        },
        receivingResults(obj) {
            //获取客户数据
            this.open = false;
            let resultObj = {};
            if (this.type == 1) {
                //今日上门  
                resultObj.id = obj.id;
                resultObj.custName = obj.custName;
                resultObj.custDesc = null;
                resultObj.candoFlag = null;
                this.todayVisitlist.push(resultObj);
            }
            if (this.type == 2) {
                //今日签约    
                resultObj.id = obj.id;
                resultObj.custName = obj.custName;
                resultObj.custDesc = null;
                resultObj.related = null;
                resultObj.relatedcustid = null;
                resultObj.relatedcustname = null;
                resultObj.relatedcustname2 = null;
                this.todaySignedlist.push(resultObj);
            }
            if (this.type == 3) {
                //今日进件
                resultObj.id = obj.id;
                resultObj.custName = obj.custName;
                resultObj.incomingId = null;
                this.todayIncomings.push(resultObj);
            }
            if (this.type == 5) {
                let signedIndex = this.todaySignedlist.indexOf(this.todaySigned);
                this.todaySignedlist[signedIndex].relatedcustid = obj.id;
                this.todaySignedlist[signedIndex].relatedcustname = obj.custName;
            }
        },
        getPublicCustSet() {
            getPublicCustSet().then(response => {
                this.resMap = response.data;
            });
        },
        delVisitCust(item) {
            //删除今日上门客户
            this.todayVisitlist.splice(this.todayVisitlist.indexOf(item), 1);

        },
        delSignedCust(item) {
            //删除今日签约客户
            this.todaySignedlist.splice(this.todaySignedlist.indexOf(item), 1);

        },
        delIncomingCust(item) {
            //删除今日进件客户
            this.todayIncomings.splice(this.todayIncomings.indexOf(item), 1);

        },
        selectCust(item) {
            //选择签约关联客户
            this.type = 5;
            if (item.relatedcustname2 == null || item.relatedcustname2 == "") {
                this.$message.error("请输入关联客户姓名或手机号，再点击查询");
                return;
            }
            this.condition = {};
            if (!/^1[3456789]\d{9}$/.test(item.relatedcustname2)) {
                this.condition.custName = item.relatedcustname2;
                this.condition.mobile = null;
            } else {
                this.condition.custName = null;
                this.condition.mobile = item.relatedcustname2;
            }
            this.open = true;
            this.title = "选择关联客户";
            this.todaySigned = item;
        },
        addIncomingCust(item){
            //录入进件
            this.custInfo.id=item.id;
            this.custInfo.custName=item.custName;
            this.incomingOpen=true;
            const childIncoming=this.$refs.childIncoming;
            if(childIncoming){
                childIncoming.getCustinfo();
            }
        },
        receivingIncoming(obj){
            //回调
            this.incomingOpen=false;
        }

    }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
.cust-box {
    width: 80%;
    margin-left: 155px;
    margin-bottom: 10px;
}

.cust-box-opera div {
    display: inline-block;
    margin-right: 10px;
}
.custName-div{
    width: 66px;
}
</style>
